package com.xm.myapplication;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.util.Log;

import com.fm.openinstall.OpenInstall;
import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.Logger;
import com.umeng.commonsdk.UMConfigure;
import com.umeng.socialize.PlatformConfig;
import com.xm.myapplication.MVP.A.Model.DB.MyObjectBox;

import androidx.multidex.MultiDex;
import io.objectbox.BoxStore;
import io.objectbox.android.AndroidObjectBrowser;

public class AppApplication extends Application {
    private BoxStore boxStore;
    private static AppApplication instance;

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        //MyObjectBox 需要建立实体类后 build
        boxStore = MyObjectBox.builder().androidContext(this).build();
        if (BuildConfig.DEBUG) {
            // 添加调试
            boolean start = new AndroidObjectBrowser(boxStore).start(this);
            Log.e("====start=======", start + "");
        }
        Log.d("App===", "Using ObjectBox " + BoxStore.getVersion() + " (" + BoxStore.getVersionNative() + ")");
        //openinstall init
        if (isMainProcess()) {
            OpenInstall.init(this);
        }
        //友盟初始化
        UMConfigure.init(this, "5dfae8624ca35758dd0001c1"
                , "umeng", UMConfigure.DEVICE_TYPE_PHONE, "");
        PlatformConfig.setQQZone("1105418748", "0JAlakouvQUNS5Iy");//QQAPPID和AppSecret

        MultiDex.install(this);
        Logger.addLogAdapter(new AndroidLogAdapter());
    }

    public static AppApplication getInstance() {
        return instance;
    }

    public BoxStore getBoxStore() {
        return boxStore;
    }


    public boolean isMainProcess() {
        int pid = android.os.Process.myPid();
        ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningAppProcessInfo appProcess : activityManager.getRunningAppProcesses()) {
            if (appProcess.pid == pid) {
                return getApplicationInfo().packageName.equals(appProcess.processName);
            }
        }
        return false;
    }
}
